home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.gsfc.nasa.gov!usenet
- From: "Thomas A. McGlynn" <tam@silk.gsfc.nasa.gov>
- Newsgroups: comp.lang.c
- Subject: Re: Tough FACTORIAL math problem...
- Date: Tue, 20 Feb 1996 17:23:44 -0500
- Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA
- Message-ID: <312A49F0.167E@silk.gsfc.nasa.gov>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com>
- <4fv74c$chq@gatekeeper.alcatel.no>
- <4fvgbu$kmb@winx03.informatik.uni-wuerzburg.de> <TANMOY.96Feb15133510@qcd.lanl.gov>
- NNTP-Posting-Host: silk.gsfc.nasa.gov
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; OSF1 V3.2 alpha)
-
- Tanmoy Bhattacharya wrote:
- >
- > In article <4fvgbu$kmb@winx03.informatik.uni-wuerzburg.de>
- > schoof@informatik.uni-wuerzburg.de (Jochen Schoof) writes:
- > <snip>
- > I'm far from being a specialist in number theory, but as can be seen from
- > my previous posting in this thread, have dealt with the problem for a while,
- > too. Allow me to give some explanation why the problem can be solved by
- > just keeping two digits stored. Assume we have two such digits m and n:
- >
- > x = 10 * m + n; with 0<=m<=9 and 1<=n<=9
- >
- > When multiplying to another number y the LSD of the result can be determined
- > by calculating LSD(n*LSD(y)) in most cases (see tabular below).
- ....
- If you think about this long enough, it becomes clear that the last digit
- then must be cyclic with a period of no longer than 40. Consider pairs of
- last digits of numbers and their factorials. The last digit of the factorial
- must be one of 2,4,6,8 (for n>1) so the combination of the last digit of
- n and the last digit of n! must repeat within a cycle of 40 iterations.
- But since the sequence of last digits of n is fixed (ie., 1234567890), and
- we're starting with the same last digit of n!, the last digits of n! must
- repeat. Thus the function should be writeable in terms of a lookup table
-
- i.e. you should be able to code it something like:
-
- if (n > CYCLE_START)
- last_digit = cycle[ (n-CYCLE_START)%CYCLE_LENGTH];
- else if (n>=0)
- last_digit = first_cycle[n];
-
- where the values of cycle and first_cycle need to be determined once and
- for all. We know that CYCLE_START <= 41 and CYCLE_LENGTH <= 40.
-
- No doubt someone else has presented this solution as well. This problem
- has generated an impressive amount of traffic....
-
- Tom McGlynn
- Goddard Space Flight Center
-